#include "crypto_common.h"

// 使用完整算法但添加详细日志的测试
void debug_full_encrypt_decrypt() {
    printf("=== 完整算法调试测试 ===\n");
    
    crypto_context_t ctx;
    unsigned char data[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
                              0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
    unsigned char backup[16];
    
    // 初始化上下文（与实际程序完全相同）
    secure_random_init();
    ctx.complexity_level = 3;
    
    // 使用固定的盐值和种子（便于调试）
    for (int i = 0; i < SALT_LENGTH; i++) {
        ctx.salt[i] = (unsigned char)(i % 256);
    }
    ctx.seed = 12345;
    
    // 使用密码生成密钥（模拟实际使用）
    const char *password = "10086";
    unsigned char hash_output[32];
    hash_function((const unsigned char*)password, strlen(password), hash_output);
    
    for (int i = 0; i < KEY_LENGTH; i++) {
        ctx.primary_key[i] = hash_output[i % 32] ^ (unsigned char)(i * 37);
    }
    
    derive_keys(&ctx);
    memcpy(backup, data, 16);
    
    printf("原始数据: ");
    for (int i = 0; i < 16; i++) printf("%02x ", data[i]);
    printf("\n");
    
    // 使用完整的加密算法（8轮）
    printf("\n=== 加密过程（8轮） ===\n");
    advanced_encrypt_block(data, 16, &ctx);
    
    printf("加密后数据: ");
    for (int i = 0; i < 16; i++) printf("%02x ", data[i]);
    printf("\n");
    
    // 使用完整的解密算法
    printf("\n=== 解密过程（8轮逆向） ===\n");
    advanced_decrypt_block(data, 16, &ctx);
    
    printf("解密后数据: ");
    for (int i = 0; i < 16; i++) printf("%02x ", data[i]);
    printf("\n");
    
    // 验证
    int success = (memcmp(data, backup, 16) == 0);
    printf("\n结果: %s\n", success ? "✓ 完整算法成功" : "✗ 完整算法失败");
    
    if (!success) {
        printf("差异详情:\n");
        for (int i = 0; i < 16; i++) {
            if (data[i] != backup[i]) {
                printf("位置 %d: 原始=0x%02x, 解密=0x%02x\n", i, backup[i], data[i]);
            }
        }
    }
}

// 测试多轮vs单轮
void test_rounds_comparison() {
    printf("\n=== 单轮vs多轮对比测试 ===\n");
    
    crypto_context_t ctx;
    unsigned char data1[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
                               0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
    unsigned char data8[16];
    unsigned char backup[16];
    
    memcpy(data8, data1, 16);
    memcpy(backup, data1, 16);
    
    // 初始化上下文
    ctx.complexity_level = 3;
    ctx.seed = 12345;
    for (int i = 0; i < SALT_LENGTH; i++) {
        ctx.salt[i] = (unsigned char)(i % 256);
    }
    for (int i = 0; i < KEY_LENGTH; i++) {
        ctx.primary_key[i] = (unsigned char)((i * 37) % 256);
    }
    derive_keys(&ctx);
    
    printf("原始数据: ");
    for (int i = 0; i < 16; i++) printf("%02x ", backup[i]);
    printf("\n");
    
    // 手动执行1轮加密解密
    printf("\n--- 手动1轮加密解密 ---\n");
    
    // 加密1轮
    int round = 0;
    xor_block(data1, ctx.derived_keys[round], 16);
    substitute_bytes(data1, 16, SBOX);
    rotate_left(data1, 16, (round + 1) * 3);
    for (size_t i = 0; i < 16; i++) {
        data1[i] ^= ctx.derived_keys[round][(i + round * 17) % KEY_LENGTH];
    }
    for (size_t i = 0; i < 16; i++) {
        unsigned char temp = data1[i];
        data1[i] = ((temp << 3) | (temp >> 5)) ^ (unsigned char)(round * 19 + i * 23);
    }
    for (size_t i = 0; i < 16; i++) {
        data1[i] ^= ctx.salt[i % SALT_LENGTH];
    }
    
    printf("1轮加密后: ");
    for (int i = 0; i < 16; i++) printf("%02x ", data1[i]);
    printf("\n");
    
    // 解密1轮
    for (size_t i = 0; i < 16; i++) {
        data1[i] ^= ctx.salt[i % SALT_LENGTH];
    }
    for (size_t i = 0; i < 16; i++) {
        data1[i] ^= (unsigned char)(round * 19 + i * 23);
        unsigned char temp = data1[i];
        data1[i] = ((temp >> 3) | (temp << 5));
    }
    for (size_t i = 0; i < 16; i++) {
        data1[i] ^= ctx.derived_keys[round][(i + round * 17) % KEY_LENGTH];
    }
    rotate_right(data1, 16, (round + 1) * 3);
    inverse_substitute_bytes(data1, 16, INV_SBOX);
    xor_block(data1, ctx.derived_keys[round], 16);
    
    printf("1轮解密后: ");
    for (int i = 0; i < 16; i++) printf("%02x ", data1[i]);
    int round1_ok = (memcmp(data1, backup, 16) == 0);
    printf(" -> %s\n", round1_ok ? "✓" : "✗");
    
    // 使用函数执行8轮加密解密
    printf("\n--- 函数8轮加密解密 ---\n");
    advanced_encrypt_block(data8, 16, &ctx);
    printf("8轮加密后: ");
    for (int i = 0; i < 16; i++) printf("%02x ", data8[i]);
    printf("\n");
    
    advanced_decrypt_block(data8, 16, &ctx);
    printf("8轮解密后: ");
    for (int i = 0; i < 16; i++) printf("%02x ", data8[i]);
    int round8_ok = (memcmp(data8, backup, 16) == 0);
    printf(" -> %s\n", round8_ok ? "✓" : "✗");
    
    printf("\n总结: 1轮=%s, 8轮=%s\n", round1_ok ? "成功" : "失败", round8_ok ? "成功" : "失败");
}

int main() {
    debug_full_encrypt_decrypt();
    test_rounds_comparison();
    return 0;
}